package com.eunion.core;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 关于ip的工具类整理.
 * 
 * 
 * @version 2013-4-19
 */
public class IpUtils {
	static Logger log = LoggerFactory.getLogger(IpUtils.class);

	/** 本机回环地址 */
	public static final String LOCAL_IP = "127.0.0.1";

	/** 本机回环地址 */
	public static String LOCAL_REAL_IP = "";

	/** vitual box */
	public static String LOCAL_VITUAL_BOX = "192.168.56";

	/** 本机广播地址 */
	public static final String LOCAL_BROADCAST_IP = "255.255.255.255";

	private IpUtils() {
		try {
			Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
			InetAddress ip = null;
			while (allNetInterfaces.hasMoreElements()) {
				NetworkInterface netInterface = allNetInterfaces.nextElement();
				Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
				while (addresses.hasMoreElements()) {
					ip = (InetAddress) addresses.nextElement();
					if (ip != null && ip instanceof Inet4Address && !ip.toString().equals(LOCAL_IP)
							&& !ip.toString().contains(LOCAL_VITUAL_BOX) && !ip.getHostAddress().equals(LOCAL_IP)) {
						LOCAL_REAL_IP = ip.getHostAddress();
						log.debug("本机的IP: " + LOCAL_REAL_IP);
						break;
					}
				}

				if (null != LOCAL_REAL_IP && !"".equals(LOCAL_REAL_IP)) {
					break;
				}

			}
		} catch (SocketException e) {
			log.error("取本机地址时出错", e);
		}

	}

	/**
	 * 将127.0.0.1形式的IP地址转换成十进制整数，这里没有进行任何错误处理 .
	 * 
	 * @return 如果为非法，返回0.
	 */
	public static long ipToLong(String strIp) {
		try {
			long[] ip = new long[4];
			// 先找到IP地址字符串中.的位置
			int position1 = strIp.indexOf(".");
			int position2 = strIp.indexOf(".", position1 + 1);
			int position3 = strIp.indexOf(".", position2 + 1);
			// 将每个.之间的字符串转换成整型
			ip[0] = Long.parseLong(strIp.substring(0, position1));
			ip[1] = Long.parseLong(strIp.substring(position1 + 1, position2));
			ip[2] = Long.parseLong(strIp.substring(position2 + 1, position3));
			ip[3] = Long.parseLong(strIp.substring(position3 + 1));
			return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
		} catch (Exception e) {
			return 0;
		}
	}

	/**
	 * 将十进制整数形式转换成127.0.0.1形式的ip地址 .
	 * 
	 * @return 如果为非法，返回空.
	 */
	public static String longToIP(long longIp) {
		try {
			StringBuffer sb = new StringBuffer("");
			// 直接右移24位
			sb.append(String.valueOf((longIp >>> 24)));
			sb.append(".");
			// 将高8位置0，然后右移16位
			sb.append(String.valueOf((longIp & 0x00FFFFFF) >>> 16));
			sb.append(".");
			// 将高16位置0，然后右移8位
			sb.append(String.valueOf((longIp & 0x0000FFFF) >>> 8));
			sb.append(".");
			// 将高24位置0
			sb.append(String.valueOf((longIp & 0x000000FF)));
			return sb.toString();
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 单例保证不重复计算.
	 */
	public static IpUtils getInstance() {
		return NestedIpUtils.instance;
	}

	// 在第一次被引用时被加载
	static class NestedIpUtils {
		private static IpUtils instance = new IpUtils();
	}

	/**
	 * 得到本机的IP地址. From
	 * /hxFire/src/main/java/com/hx/methodPlatform/util/GetIp.java.
	 * 
	 * @return 返回 本机的IP地址,如果不正确将返回255.255.255.255
	 */
	public String getLocalIp() {
		return LOCAL_REAL_IP;
	}
}
